# -*- coding: utf-8 -*-
"""
Created on Thu May 13 10:20:22 2021

@author: Hu Yue
@email: hhhuyue@gmail.com

Note:
"""

import pandas as pd
from hedge_utils import get_hedged_retDT1

# load data
option_trade = pd.read_csv("domestic data/options_trade_all.csv",usecols=['date','contract_code','close','iv','delta','vega','gamma','theta','vol','rho'])
option_trade['date'] = pd.to_datetime(option_trade['date'])

future_trade = pd.read_csv("domestic data/futures_trade_all.csv",usecols=['date','code','close','va_type'])
future_trade['date'] = pd.to_datetime(future_trade['date'])

# load data of informations
option_info = pd.read_csv("domestic data/options_info_all.csv",usecols=['contract_code','object_code','strike_price','strike_way','final_date'])
option_info = option_info.drop_duplicates(subset = ['contract_code'])
option_info.loc[:,'cp_type'] = option_info['contract_code'].apply(lambda x: (x.split("-")[1]=='C')*1)
option_info['final_date'] = pd.to_datetime(option_info['final_date'],format='%Y%m%d')



# data preprocessing
option_trade_info = pd.merge(option_trade,option_info,how='left',on='contract_code')
#option_trade_info.loc[:,'time2mat'] = (pd.eval('option_trade_info.final_date - option_trade_info.date')).dt.days


#%%
# 提取豆粕和玉米的数据
import numpy as np
future_trade = future_trade[future_trade['va_type'].str[0:2]!="生猪"]
future_trade.sort_values(by=['code','date'],inplace=True)

future_trade.loc[:,'diff_1'] = future_trade.groupby(['code']).close.diff(1)
future_trade.loc[:,'diff_2'] = future_trade.groupby(['code']).close.diff(2)
future_trade.loc[:,'diff_3'] = future_trade.groupby(['code']).close.diff(3)
future_trade.loc[:,'diff_1_ratio'] = future_trade.groupby(['code']).close.pct_change(1)
future_trade.loc[:,'diff_2_ratio'] = future_trade.groupby(['code']).close.pct_change(2)
future_trade.loc[:,'diff_3_ratio'] = future_trade.groupby(['code']).close.pct_change(3)
future_trade.loc[:,'rank_1'] = np.abs(future_trade['diff_1_ratio'])
future_trade.loc[:,'rank_2'] = np.abs(future_trade['diff_2_ratio'])
future_trade.loc[:,'rank_3'] = np.abs(future_trade['diff_3_ratio'])

future_trade.sort_values(by=['code','rank_1'],inplace=True,ascending=False)
final_data_1 = future_trade[['date','code','va_type','close','diff_1','diff_1_ratio']].groupby(['code']).head(10)
future_trade.sort_values(by=['code','rank_2'],inplace=True,ascending=False)
final_data_2 = future_trade[['date','code','va_type','close','diff_1','diff_2_ratio']].groupby(['code']).head(10)
future_trade.sort_values(by=['code','rank_3'],inplace=True,ascending=False)
final_data_3 = future_trade[['date','code','va_type','close','diff_1','diff_3_ratio']].groupby(['code']).head(10)
#%%
temp = future_trade[['date','code','va_type','close','diff_3','rank_3']][future_trade['rank_3']>=0.1]

#%%
future_trade.sort_values(by=['rank_3'],inplace=True,ascending=False)
temp = future_trade[['date','code','va_type','close','diff_3','rank_3']].head(10)

#%%
future_trade.sort_values(by=['va_type','rank_3'],inplace=True,ascending=False)
temp = future_trade[['date','code','va_type','close','diff_3','rank_3']].groupby(['va_type']).head(1)